<?php

namespace openapi\controllers;

use peng\helpers\ArrayHelper;
use peng\helpers\Config;

/**
 * 验证码
 *
 * @author peng.huang
 */
class VerifycodeController extends \openapi\controllers\base\OpenAPIController {

    /**
     * 初始化
     */
    public function init() {
        
    }

    /**
     * 生成验证码图片
     */
    public function actionIndex() {
        $captcha = new \peng\libraries\Captcha();
        $captcha->outputImage(array($this, 'writeVerifyCode'), 4, 15);
    }

    /**
     * 将验证码写入cookie
     * 
     * @param string $code [验证码]
     */
    public function writeVerifyCode($code) {
        $config = Config::get('cookie', 'account');
        $cookieName = ArrayHelper::get($config, 'reg_code_name');
        $encryptKey = ArrayHelper::get($config, 'encryption_key');
        $this->response->addCookie($cookieName, md5(strtolower($code) . $encryptKey), 300); // 5分钟内有效
    }

}
